var express = require('express'),
  router = express.Router();
  mongoose = require('mongoose'),
  Post = mongoose.model('Post'),
  User = mongoose.model('User'),
  Category = mongoose.model('Category');

// 调用分页helper
var pageHelper = require('../../helper/page');

module.exports = function (app) {
  app.use('/article', router);
};

// 文章列表
// router.get('/', function(req, res, next) {
//   Post.find()
//       .populate('category')
//       .populate('author')
//       .sort({created:'desc'}) // desc 倒序   asc 正序
//       .exec(function (err, posts) {
//         if (err) return next(err);
//         res.render('backend/article', {
//           title: '文章列表页',
//           posts: posts
//         });
//       });
// });

router.get('/', function(req, res, next) {
  // 获取传递page
  var page = req.query.page || 1;
  pageHelper.pageQuery(page,2,Post,['category','author'],'',{created: 'desc'}, function (err, $page){
    if (err) {
      return next(err);
    } else {
      res.render('backend/article',{
        title: '文章列表页',
        posts: $page.results,
        pageNumber: $page.pageNumber,
        pageCount: $page.pageCount
      });
    }
  });
});


// 新增文章
router.get('/add', function(req, res, next) {
  res.render('backend/article_add', {
    title: '新增文章',
    action: '/article/add'
  });
});

router.post('/add', function(req, res, next) {
  var title = req.body.title,
      category = req.body.category,
      body = req.body.body;
  User.findOne({}, function (err, author) {
    if (err) return next(err);
    var post = new Post({
      title: title,
      body: body,
      category: category,
      author: author,
      slug: title,
      published: true,
      meta: {favourates:0},
      comments: [],
      created: new Date()
    });
    post.save(function (err, post) {
        if (err) {
          req.flash('error', '文章保存失败');
          res.redirect('/article/add');
        } else {
          req.flash('error', '文章保存成功');
          res.redirect('/article');
        }
    });
  });
});

// 编辑文章
router.get('/edit/:id', function (req, res, next) {
    var id = req.params.id;
    if (!id) {
      return next(new Error('not found id !'));
    }
    Post.findOne({_id:id}).populate('category').populate('author').exec(function (err, post){
      if (err) return next(err);
      res.render('backend/article_add', {
        title: '编辑文章',
        post: post,
        action: '/article/edit',
      });
    })
});

// 修改文章
router.post('/edit', function (req, res, next) {
    var title = req.body.title,
        category = req.body.category,
        body = req.body.bodyl;
    if (!id) return next(new Error('not found id'));
    Post.findOne({_id:id}).populate('category').populate('author').exec(function (err, post) {
      if (err) return next(err);
      post.title = title;
      post.category = category;
      post.body = body;
      post.save(function(err) {
        if (err) return nex(err);
        // 跳转回列表页
        res.resdirect('/article');
      });
    });
});

// 删除文章
router.get('/del/:id', function(req, res, next) {
  var id = req.params.id;
  if (!id) return next(new Error('no id in post'));
  Post.remove({_id:id}, function (err, posts) {
    if (err) return next(err);
    console.log(posts);
    res.redirect('/article');
  });
});
